/**
 * Created by IntelliJ IDEA.
 * User: p.popov
 * Date: 27.03.13
 * Time: 4:16
 * To change this template use File | Settings | File Templates.
 */
package slicesPanels {
	
import flash.geom.Point;
import slicesPanels.*
import SimpleImage;


public class Slice3Panel extends CSprite {
    private var settings:Slice3PanelSettings;
    private var leftPart:SimpleImage;
    private var rightPart:SimpleImage;
    private var centerPart:SimpleImage;
    private var innerWidth:Number;
    private var innerHeight:Number;
    private var atlasPath: String;

    private var loadCallback : Function;

    public function Slice3Panel(settings:Slice3PanelSettings, atlasPath:String) {
        this.settings = settings;
        this.atlasPath = atlasPath;
        init();
    }

    private function init():void {
        createCenterPart();
        createLeftPart();
        createRightPartImage();
    }

    private function createLeftPart():void {
        /*leftPart = GuiFactory.createSimpleImage(settings.leftCorner, atlastPath);
        leftPart.setLoadCallback(onResourceLoaded);
        addChild(leftPart);*/

        var resId:String = settings.leftCorner;
        var leftPartAbsent:Boolean = (resId == null);
        var leftPartScaleX:int = 1;
        if (leftPartAbsent) {
            resId = settings.rightCorner;
            leftPartScaleX = -1;
        }
        leftPart = AtlasHolder.instance.createSimpleImage(resId, atlasPath);
        leftPart.scaleX = leftPartScaleX;
        leftPart.setLoadCallback(onResourceLoaded);
        addChild(leftPart);
    }

    private function createCenterPart():void {
		/*if (!settings.hasCenter()) {
			centerPart = new SimpleImage("");
			return;
		}*/
        centerPart = AtlasHolder.instance.createSimpleImage(settings.center, atlasPath);
        centerPart.setLoadCallback(onResourceLoaded);
        addChild(centerPart);
    }

    private function onResourceLoaded():void {
        if ( !isLoaded() ) return;

        leftPart.pivotX = leftPart.scaleX < 0 ? leftPart.width : 0;
        rightPart.pivotX = rightPart.scaleX < 0 ? rightPart.width : 0;
        if (!innerWidth) {
            innerWidth = leftPart.width + centerPart.width + rightPart.width;
        }
        if (!innerHeight) {
            innerHeight = leftPart.height;
        }
        readjustSize();

        if ( loadCallback != null ) {
            loadCallback();
        }
    }

    /**
     * setSize приведет к вызову setSize2
     * */
    override public function setSize(size:Size):void {
        innerWidth = size.width;
        innerHeight = size.height;
        if (isLoaded()) {
            if (innerWidth == 0 || innerHeight == 0) {
                visible = false;
                return;
            }
            leftPart.setScale(1,1);
            rightPart.setScale(1,1);
            visible = true;
            readjustSize();
        }
    }

    override public function getSize():Size {
        return new Size(innerWidth * scaleX, innerHeight * scaleY);
    }

    public function setLoadCallback( loadCallback : Function ) : void {
        this.loadCallback = loadCallback;
        if ( isLoaded() ) {
            if ( loadCallback != null) {
                loadCallback();
            }
        }
    }

    private function readjustSize():void {
        var innerScale:Number = Math.min(innerHeight / leftPart.height);
        var leftScale: Number = settings.leftCorner ? innerScale : -innerScale;
        leftPart.setScale(leftScale, innerScale);
        var rightScale: Number = settings.rightCorner ? innerScale : -innerScale;
        rightPart.setScale(rightScale, innerScale);
        calculateCenterPartWidth();
        arrange();
    }

    private function calculateCenterPartWidth():void {
        centerPart.setSize(Math.max((innerWidth - (leftPart.width + rightPart.width)), 0), innerHeight);
    }

    private function arrange():void {
        leftPart.setPosition(0, 0);
        centerPart.setPosition(leftPart.x + leftPart.width, 0);
        rightPart.setPosition(centerPart.x + centerPart.width, 0);
    }

    private function createRightPartImage():void {
        var resId:String = settings.rightCorner;
        var rightPartAbsent:Boolean = (resId == null);
        var rightPartScaleX:int = 1;
        if (rightPartAbsent) {
            resId = settings.leftCorner;
            rightPartScaleX = -1;
        }
        rightPart = AtlasHolder.instance.createSimpleImage(resId, atlasPath);
        rightPart.scaleX = rightPartScaleX;
        rightPart.setLoadCallback(onResourceLoaded);
        addChild(rightPart);
    }


    override public function set width(width:Number):void {
        setSize(new Size(width, innerHeight));
    }


    override public function set height(height:Number):void {
        setSize(new Size(innerWidth, height));
    }

    public function isLoaded():Boolean {
        return leftPart && leftPart.isLoaded() && centerPart && centerPart.isLoaded() && rightPart && rightPart.isLoaded();
    }

}
}
